package cn.rongdao.jrkabao.ui.cashadvance.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mifengkong.frtools.inputmethod.FRView;
import com.mifengkong.frtools.util.FRString;

import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.SelectBankCardBean;
import cn.rongdao.jrkabao.ui.cashadvance.uicomponent.SelectBankCardUIComponent;

/**
 * Created by jack on 2017/8/8
 */

public class SelectBankCardAdapter extends RecyclerView.Adapter<SelectBankCardAdapter.SelectBankCardViewHolder> implements StickyHeaderAdapter<SelectBankCardAdapter.HeaderViewHolder> {

    private Context context;
    private List<SelectBankCardBean> mList;
    private char lastChar = '\u0000';
    private int DisplayIndex = 0;
    private OnSelectBankCardClickListener onSelectBankCardClickListener;

    public SelectBankCardAdapter(Context context, List<SelectBankCardBean> list) {
        this.context = context;
        this.mList = list;
    }

    @Override
    public SelectBankCardViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectBankCardViewHolder(FRView.inflateLayout(context, R.layout.item_select_bank_card, parent, false));
    }

    @Override
    public void onBindViewHolder(SelectBankCardViewHolder holder, final int position) {
        holder.selectBankCardUIComponent.setSelectBankCardContent(mList.get(position));
        if (position == 0) {
            holder.selectBankCardUIComponent.setViewLineVisible(View.GONE);
        } else {
            SelectBankCardBean currentItem = mList.get(position);
            SelectBankCardBean lastItem = mList.get(position - 1);
            if (!currentItem.getPinyinFirst().equals(lastItem.getPinyinFirst())) {
                holder.selectBankCardUIComponent.setViewLineVisible(View.GONE);
            } else {
                holder.selectBankCardUIComponent.setViewLineVisible(View.VISIBLE);
            }
        }
        holder.selectBankCardUIComponent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onSelectBankCardClickListener) {
                    onSelectBankCardClickListener.click(v, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public int getPositionForSection(char pinyinFirst) {
        for (int i = 0; i < getItemCount(); i++) {
            char firstChar = mList.get(i).getPinyinFirst().charAt(0);
            if (firstChar == pinyinFirst) {
                return i;
            }
        }
        return -1;
    }

    public void setOnSelectBankCardClickListener(OnSelectBankCardClickListener onSelectBankCardClickListener) {
        this.onSelectBankCardClickListener = onSelectBankCardClickListener;
    }

    ///////////////////////////////////////////////////////////悬浮栏///////////////////////////////////////////////////////////
    @Override
    public long getHeaderId(int position) {
        //这里面的是如果当前position与之前position重复（内部判断）  则不显示悬浮标题栏  如果不一样则显示标题栏
        if (null != mList.get(position) && mList.get(position).getPinyinFirst().charAt(0) != '\0') {
            char ch = mList.get(position).getPinyinFirst().charAt(0);
            if (lastChar == '\u0000') {
                lastChar = ch;
                return DisplayIndex;
            } else {
                if (lastChar == ch) {
                    return DisplayIndex;
                } else {
                    lastChar = ch;
                    DisplayIndex++;
                    return DisplayIndex;
                }

            }
        } else {
            return DisplayIndex;
        }
    }

    @Override
    public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return new HeaderViewHolder(FRView.inflateLayout(context, R.layout.item_select_bank_head, parent, false));
    }

    @Override
    public void onBindHeaderViewHolder(HeaderViewHolder viewholder, int position) {
        if (mList.get(position).getPinyinFirst().charAt(0) == '\0') {
            viewholder.tvHeader.setText("#");
        } else {
            viewholder.tvHeader.setText(FRString.valueOf(mList.get(position).getPinyinFirst()));
        }
    }

    public class SelectBankCardViewHolder extends RecyclerView.ViewHolder {

        public SelectBankCardUIComponent selectBankCardUIComponent;

        public SelectBankCardViewHolder(View itemView) {
            super(itemView);
            selectBankCardUIComponent = (SelectBankCardUIComponent) itemView;
        }
    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {

        public TextView tvHeader;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            tvHeader = (TextView) itemView;
        }
    }

    public interface OnSelectBankCardClickListener {
        void click(View v, int position);
    }
}
